<!Doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>C++ 常量 | 菜鸟教程</title>

  <meta name='robots' content='max-image-preview:large' />
<link rel='stylesheet' id='classic-theme-styles-css' href='../0/classic-themes.min.css?ver=6.2' type='text/css' media='all' />
<link rel="canonical" href="../cplusplus/cpp-constants-literals.html" />
<meta name="keywords" content="C++ 常量">
<meta name="description" content="C++ 常量   常量是固定值，在程序执行期间不会改变。这些固定的值，又叫做字面量。 常量可以是任何的基本数据类型，可分为整型数字、浮点数字、字符、字符串和布尔值。 常量就像是常规的变量，只不过常量的值在定义后不能进行修改。   整数常量 整数常量可以是十进制、八进制或十六进制的常量。前缀指定基数：0x 或 0X 表示十六进制，0 表示八进制，不带前缀则默认表示十进制。 整数常量也可以带一个后缀，后缀是 U 和 L 的组合，U 表示无符..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico">
	<link rel="stylesheet" href="../0/style.css?v=1.170" type="text/css" media="all" />	
	<link rel="stylesheet" href="../0/font-awesome.min.css" media="all" />	
  <!--[if gte IE 9]><!-->
  <script src=""></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src=""></script>
     <script src=""></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
      
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="../">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        
    <div class="col search search-desktop last">
      <div class="search-input" >
      <form action="//www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……"  autocomplete="off" style="height: 44px;">
      </form>
      
      </div>
    </div>
  </div>
</div>



<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav" id="runoob-detail-nav">
				<li><a href="../">首页</a></li>
				<li><a href="../html/html-tutorial.html">HTML</a></li>
				<li><a href="../css/css-tutorial.html">CSS</a></li>
				<li><a href="../js/js-tutorial.html">JavaScript</a></li>
				<li><a href="javascript:void(0);" data-id="vue">Vue</a></li>
				<li><a href="javascript:void(0);" data-id="bootstrap">Bootstrap</a></li>
				<li><a href="../nodejs/nodejs-tutorial.html">NodeJS</a></li>
				<li><a href="../python3/python3-tutorial.html">Python3</a></li>
				<li><a href="../python/python-tutorial.html">Python2</a></li>
				<li><a href="../java/java-tutorial.html">Java</a></li>
				<li><a href="../cprogramming/c-tutorial.html">C</a></li>
				<li><a href="../cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="../csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="../go/go-tutorial.html">Go</a></li>
				<li><a href="../sql/sql-tutorial.html">SQL</a></li>
				<li><a href="../linux/linux-tutorial.html">Linux</a></li>
				<li><a href="../jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="../browser-history">本地书签</a></li>
				<!--
			
				<li><a href="../w3cnote/knowledge-start.html" style="font-weight: bold;" onclick="_hmt.push(['_trackEvent', '星球', 'click', 'start'])" title="我的圈子">我的圈子</a></li>				
				<li><a href="javascript:;" class="runoob-pop">登录</a></li>
				-->
      		</ul>
			<ul class="mobile-nav">
				<li><a href="../">首页</a></li>
				<li><a href="../html/html-tutorial.html">HTML</a></li>
				<li><a href="../css/css-tutorial.html">CSS</a></li>
				<li><a href="../js/js-tutorial.html">JS</a></li>
				<li><a href="../browser-history">本地书签</a></li>
				<li><a href="javascript:void(0)" class="search-reveal">Search</a> </li>
			</ul>
			
		</div>
	</div>
</div>

<div class="container sub-navigation sub-navigation-articles" style="display:none">
	<div class="row">
		<div class="col nav-sub">
		<ul id="python">
			<li class="cat-item"><a href="../python3/python3-tutorial.html">Python3 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../python/python-tutorial.html">Python2 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
		</ul>
		<ul id="vue">
			<li class="cat-item"><a href="../vue3/vue3-tutorial.html">Vue3 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../vue/vue-tutorial.html">vue2 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
		</ul>

		<ul id="bootstrap">
			<li class="cat-item"><a href="../bootstrap/bootstrap-tutorial.html">Bootstrap3 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../bootstrap4/bootstrap4-tutorial.html">Bootstrap4 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../bootstrap5/bootstrap5-tutorial.html">Bootstrap5 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../bootstrap/bootstrap-v2-tutorial.html">Bootstrap2 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
		</ul>
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="runoob-col-md2">
<div class="left-column">
	<div class="tab" style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
	<i class="fa fa-list" aria-hidden="true"></i> 
	<span>C++ 教程</span>
	<a data-cate="166" href="javascript:void(0);" title="夜间模式"  id="moon"><i class="fa fa-moon-o" aria-hidden="true" style="line-height: 28px;font-size: 1.2em;margin: 2px 4px 0;"></i></a>
	<a data-cate="166" style="display:none;" href="javascript:void(0);" title="日间模式"  id="sun" ><i class="fa fa-sun-o" aria-hidden="true" style="line-height: 28px;font-size: 1.2em;margin: 2px 4px 0;line-height: 28px;
}"></i></a>

	</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="C++ 教程"  href="../cplusplus/cpp-tutorial.html" >
			C++ 教程			</a>
						<a target="_top" title="C++ 简介"  href="../cplusplus/cpp-intro.html" >
			C++ 简介			</a>
						<a target="_top" title="C++ 环境设置"  href="../cplusplus/cpp-environment-setup.html" >
			C++ 环境设置			</a>
						<a target="_top" title="C++ 基本语法"  href="../cplusplus/cpp-basic-syntax.html" >
			C++ 基本语法			</a>
						<a target="_top" title="C++ 注释"  href="../cplusplus/cpp-comments.html" >
			C++ 注释			</a>
						<a target="_top" title="C++ 数据类型"  href="../cplusplus/cpp-data-types.html" >
			C++ 数据类型			</a>
						<a target="_top" title="C++ 变量类型"  href="../cplusplus/cpp-variable-types.html" >
			C++ 变量类型			</a>
						<a target="_top" title="C++ 变量作用域"  href="../cplusplus/cpp-variable-scope.html" >
			C++ 变量作用域			</a>
						<a target="_top" title="C++ 常量"  href="../cplusplus/cpp-constants-literals.html" >
			C++ 常量			</a>
						<a target="_top" title="C++ 修饰符类型"  href="../cplusplus/cpp-modifier-types.html" >
			C++ 修饰符类型			</a>
						<a target="_top" title="C++ 存储类"  href="../cplusplus/cpp-storage-classes.html" >
			C++ 存储类			</a>
						<a target="_top" title="C++ 运算符"  href="../cplusplus/cpp-operators.html" >
			C++ 运算符			</a>
						<a target="_top" title="C++ 循环"  href="../cplusplus/cpp-loops.html" >
			C++ 循环			</a>
						<a target="_top" title="C++ 判断"  href="../cplusplus/cpp-decision.html" >
			C++ 判断			</a>
						<a target="_top" title="C++ 函数"  href="../cplusplus/cpp-functions.html" >
			C++ 函数			</a>
						<a target="_top" title="C++ 数字"  href="../cplusplus/cpp-numbers.html" >
			C++ 数字			</a>
						<a target="_top" title="C++ 数组"  href="../cplusplus/cpp-arrays.html" >
			C++ 数组			</a>
						<a target="_top" title="C++ 字符串"  href="../cplusplus/cpp-strings.html" >
			C++ 字符串			</a>
						<a target="_top" title="C++ 指针"  href="../cplusplus/cpp-pointers.html" >
			C++ 指针			</a>
						<a target="_top" title="C++ 引用"  href="../cplusplus/cpp-references.html" >
			C++ 引用			</a>
						<a target="_top" title="C++ 日期 &#038; 时间"  href="../cplusplus/cpp-date-time.html" >
			C++ 日期 & 时间			</a>
						<a target="_top" title="C++ 基本的输入输出"  href="../cplusplus/cpp-basic-input-output.html" >
			C++ 基本的输入输出			</a>
						<a target="_top" title="C++ 数据结构"  href="../cplusplus/cpp-data-structures.html" >
			C++ 数据结构			</a>
			<br><h2 class="left"><span class="left_h2">C++</span> 面向对象</h2>			<a target="_top" title="C++ 类 &#038; 对象"  href="../cplusplus/cpp-classes-objects.html" >
			C++ 类 & 对象			</a>
						<a target="_top" title="C++ 继承"  href="../cplusplus/cpp-inheritance.html" >
			C++ 继承			</a>
						<a target="_top" title="C++ 重载运算符和重载函数"  href="../cplusplus/cpp-overloading.html" >
			C++ 重载运算符和重载函数			</a>
						<a target="_top" title="C++ 多态"  href="../cplusplus/cpp-polymorphism.html" >
			C++ 多态			</a>
						<a target="_top" title="C++ 数据抽象"  href="../cplusplus/cpp-data-abstraction.html" >
			C++ 数据抽象			</a>
						<a target="_top" title="C++ 数据封装"  href="../cplusplus/cpp-data-encapsulation.html" >
			C++ 数据封装			</a>
						<a target="_top" title="C++ 接口（抽象类）"  href="../cplusplus/cpp-interfaces.html" >
			C++ 接口（抽象类）			</a>
			<br><h2 class="left"><span class="left_h2">C++</span> 高级教程</h2>			<a target="_top" title="C++ 文件和流"  href="../cplusplus/cpp-files-streams.html" >
			C++ 文件和流			</a>
						<a target="_top" title="C++ 异常处理"  href="../cplusplus/cpp-exceptions-handling.html" >
			C++ 异常处理			</a>
						<a target="_top" title="C++ 动态内存"  href="../cplusplus/cpp-dynamic-memory.html" >
			C++ 动态内存			</a>
						<a target="_top" title="C++ 命名空间"  href="../cplusplus/cpp-namespaces.html" >
			C++ 命名空间			</a>
						<a target="_top" title="C++ 模板"  href="../cplusplus/cpp-templates.html" >
			C++ 模板			</a>
						<a target="_top" title="C++ 预处理器"  href="../cplusplus/cpp-preprocessor.html" >
			C++ 预处理器			</a>
						<a target="_top" title="C++ 信号处理"  href="../cplusplus/cpp-signal-handling.html" >
			C++ 信号处理			</a>
						<a target="_top" title="C++ 多线程"  href="../cplusplus/cpp-multithreading.html" >
			C++ 多线程			</a>
						<a target="_top" title="C++ Web 编程"  href="../cplusplus/cpp-web-programming.html" >
			C++ Web 编程			</a>
			<br><h2 class="left"><span class="left_h2">C++</span> 资源库</h2>			<a target="_top" title="C++ STL 教程"  href="../cplusplus/cpp-stl-tutorial.html" >
			C++ STL 教程			</a>
						<a target="_top" title="C++ 标准库"  href="../cplusplus/cpp-standard-library.html" >
			C++ 标准库			</a>
						<a target="_top" title="C++ 有用的资源"  href="../cplusplus/cpp-useful-resources.html" >
			C++ 有用的资源			</a>
						<a target="_top" title="C++ 实例"  href="../cplusplus/cpp-examples.html" >
			C++ 实例			</a>
						<a target="_top" title="C++ 测验"  href="../cplusplus/cpp-quiz.html" >
			C++ 测验			</a>
				
		</div>
	</div>	
</div>
</div>	<div class="col middle-column">
		
	
	<div class="article">
		
		<div class="previous-next-links">
			<div class="previous-design-link"><i style="font-size:16px;" class="fa fa-arrow-left" aria-hidden="true"></i> <a href="../cplusplus/cpp-variable-scope.html" rel="prev"> C++ 变量作用域</a> </div>
			<div class="next-design-link"><a href="../cplusplus/cpp-modifier-types.html" rel="next"> C++ 修饰符类型</a> <i style="font-size:16px;" class="fa fa-arrow-right" aria-hidden="true"></i></div>
		</div>
<div class="article-heading-ad" style="display: block;">
<script async src=""
     crossorigin="anonymous"></script>
<!-- 728*90 固定 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-5751451760833794"
     data-ad-slot="2147566827"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			
<h1>C++ <span class="color_h1">常量</span></h1>

<div class="tutintro">
<p>常量是固定值，在程序执行期间不会改变。这些固定的值，又叫做<b>字面量</b>。</p>
<p>常量可以是任何的基本数据类型，可分为整型数字、浮点数字、字符、字符串和布尔值。</p>
<p>常量就像是常规的变量，只不过常量的值在定义后不能进行修改。</p>
</div>

<h2 class="tutheader">整数常量</h2>
<p>整数常量可以是十进制、八进制或十六进制的常量。前缀指定基数：0x 或 0X 表示十六进制，0 表示八进制，不带前缀则默认表示十进制。</p>
<p>整数常量也可以带一个后缀，后缀是 U 和 L 的组合，U 表示无符号整数（unsigned），L 表示长整数（long）。后缀可以是大写，也可以是小写，U 和 L 的顺序任意。</p>
<p>下面列举几个整数常量的实例：</p>
<div class="example"> 
<div class="example_code">
<div class="hl-main"><span class="hl-number">212</span><span class="hl-code">         </span><span class="hl-comment">// 合法的</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">215</span><span class="hl-identifier">u</span><span class="hl-code">        </span><span class="hl-comment">// 合法的</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">0xFee</span><span class="hl-identifier">L</span><span class="hl-code">      </span><span class="hl-comment">// 合法的</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">078</span><span class="hl-code">         </span><span class="hl-comment">// 非法的：8 不是八进制的数字</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">032</span><span class="hl-identifier">UU</span><span class="hl-code">       </span><span class="hl-comment">// 非法的：不能重复后缀</span></div>
</div>
</div>
<p>以下是各种类型的整数常量的实例：</p>
<div class="example"> 
<div class="example_code">
<div class="hl-main"><span class="hl-number">85</span><span class="hl-code">         </span><span class="hl-comment">// 十进制</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">0213</span><span class="hl-code">       </span><span class="hl-comment">// 八进制 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">0x4b</span><span class="hl-code">       </span><span class="hl-comment">// 十六进制 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">30</span><span class="hl-code">         </span><span class="hl-comment">// 整数 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">30</span><span class="hl-identifier">u</span><span class="hl-code">        </span><span class="hl-comment">// 无符号整数 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">30</span><span class="hl-identifier">l</span><span class="hl-code">        </span><span class="hl-comment">// 长整数 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">30</span><span class="hl-identifier">ul</span><span class="hl-code">       </span><span class="hl-comment">// 无符号长整数</span></div>
</div>
</div>

<h2 class="tutheader">浮点常量</h2>
<p>浮点常量由整数部分、小数点、小数部分和指数部分组成。您可以使用小数形式或者指数形式来表示浮点常量。</p>
<p>当使用小数形式表示时，必须包含整数部分、小数部分，或同时包含两者。当使用指数形式表示时， 必须包含小数点、指数，或同时包含两者。带符号的指数是用 e 或 E 引入的。</p>
<p>下面列举几个浮点常量的实例：</p>
<div class="example"> 
<div class="example_code">
<div class="hl-main"><span class="hl-number">3</span><span class="hl-number">.14159</span><span class="hl-code">       </span><span class="hl-comment">// 合法的 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">314159</span><span class="hl-identifier">E</span><span class="hl-code">-</span><span class="hl-number">5</span><span class="hl-identifier">L</span><span class="hl-code">    </span><span class="hl-comment">// 合法的 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">510</span><span class="hl-identifier">E</span><span class="hl-code">          </span><span class="hl-comment">// 非法的：不完整的指数</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-number">210</span><span class="hl-identifier">f</span><span class="hl-code">          </span><span class="hl-comment">// 非法的：没有小数或指数</span><span class="hl-comment"></span><span class="hl-code">
.</span><span class="hl-identifier">e55</span><span class="hl-code">          </span><span class="hl-comment">// 非法的：缺少整数或分数</span></div>
</div>
</div>

<h2 class="tutheader">布尔常量</h2>
<p>布尔常量共有两个，它们都是标准的 C++ 关键字：</p>
<ul class="list">
<li><b>true</b> 值代表真。</li>
<li><b>false</b> 值代表假。</li>
</ul>
<p>我们不应把 true 的值看成 1，把 false 的值看成 0。</p>

<h2 class="tutheader">字符常量</h2>
<p>字符常量是括在单引号中。如果常量以 L（仅当大写时）开头，则表示它是一个宽字符常量（例如 L'x'），此时它必须存储在 <b>wchar_t</b> 类型的变量中。否则，它就是一个窄字符常量（例如 'x'），此时它可以存储在 <b>char</b> 类型的简单变量中。</p>
<p>字符常量可以是一个普通的字符（例如 'x'）、一个转义序列（例如 '\t'），或一个通用的字符（例如 '\u02C0'）。</p>
<p>在 C++ 中，有一些特定的字符，当它们前面有反斜杠时，它们就具有特殊的含义，被用来表示如换行符（\n）或制表符（\t）等。下表列出了一些这样的转义序列码：</p>
<table class="reference notranslate">
<tr><th>转义序列</th><th>含义</th></tr>
<tr><td>\\</td><td>\ 字符</td></tr>
<tr><td>\'</td><td> ' 字符</td></tr>
<tr><td>\"</td><td>" 字符</td></tr>
<tr><td>\?</td><td>? 字符</td></tr>
<tr><td>\a</td><td>警报铃声</td></tr>
<tr><td>\b</td><td>退格键</td></tr>
<tr><td>\f</td><td>换页符</td></tr>
<tr><td>\n</td><td>换行符</td></tr>
<tr><td>\r</td><td>回车</td></tr>
<tr><td>\t</td><td>水平制表符</td></tr>
<tr><td>\v</td><td>垂直制表符</td></tr>
<tr><td>\ooo</td><td>一到三位的八进制数</td></tr>
<tr><td>\xhh . . .</td><td>一个或多个数字的十六进制数</td></tr>
</table>
<p>下面的实例显示了一些转义序列字符：</p>
<div class="example"> 
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-reserved">using</span><span class="hl-code"> </span><span class="hl-types">namespace</span><span class="hl-code"> </span><span class="hl-identifier">std</span><span class="hl-code">;
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-identifier">cout</span><span class="hl-code"> &lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello</span><span class="hl-special">\</span><span class="hl-string">tWorld</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-special">\</span><span class="hl-string">n</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>当上面的代码被编译和执行时，它会产生下列结果：</p>
<pre>
Hello   World
</pre>

<h2 class="tutheader">字符串常量</h2>
<p>字符串字面值或常量是括在双引号 <span class="marked">""</span> 中的。一个字符串包含类似于字符常量的字符：普通的字符、转义序列和通用的字符。</p>
<p>您可以使用 <span class="marked">\</span> 做分隔符，把一个很长的字符串常量进行分行。</p>
<p>下面的实例显示了一些字符串常量：</p>

<div class="example"><h2 class="example">实例</h2> <div class="example_code">
<span style="color: #060;">#include &lt;iostream&gt;</span><br />
<span style="color: #060;">#include &lt;string&gt;</span><br />
<span style="color: #05a;">using</span> <span style="color: #05a;">namespace</span> std<span style="color: #008080;">;</span><br />
<br />
<span style="color: #05a;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; string greeting <span style="color: #000080;">=</span> <span style="color: #a11;">&quot;hello, runoob&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #05a;">cout</span> <span style="color: #000080;">&lt;&lt;</span> greeting<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #05a;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #a11;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span> &nbsp; &nbsp; <span style="color: #666666;">// 换行符</span><br />
&nbsp; &nbsp; string greeting2 <span style="color: #000080;">=</span> <span style="color: #a11;">&quot;hello, <span style="color: #000099; font-weight: bold;">\<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;runoob&quot;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #05a;">cout</span> <span style="color: #000080;">&lt;&lt;</span> greeting2<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #05a;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><br />
</div></div>


<pre>
hello, runoob
hello, runoob
</pre>

<h2 class="tutheader">定义常量</h2>
<p>在 C++ 中，有两种简单的定义常量的方式：</p>
<ul class="list">
<li>使用 <b>#define</b> 预处理器。</li>
<li>使用 <b>const</b> 关键字。</li>
</ul>
<h2>#define 预处理器</h2>
<p>下面是使用 #define 预处理器定义常量的形式：</p>
<pre>
#define identifier value
</pre>
<p>具体请看下面的实例：</p>
<div class="example"> 
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-reserved">using</span><span class="hl-code"> </span><span class="hl-types">namespace</span><span class="hl-code"> </span><span class="hl-identifier">std</span><span class="hl-code">;
 
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">LENGTH</span><span class="hl-code"> </span><span class="hl-number">10</span><span class="hl-code">   </span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">WIDTH</span><span class="hl-code">  </span><span class="hl-number">5</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">NEWLINE</span><span class="hl-code"> '\</span><span class="hl-identifier">n</span><span class="hl-code">'</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
 
   </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">area</span><span class="hl-code">;  
   
   </span><span class="hl-identifier">area</span><span class="hl-code"> = </span><span class="hl-identifier">LENGTH</span><span class="hl-code"> * </span><span class="hl-identifier">WIDTH</span><span class="hl-code">;
   </span><span class="hl-identifier">cout</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">area</span><span class="hl-code">;
   </span><span class="hl-identifier">cout</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">NEWLINE</span><span class="hl-code">;
   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>当上面的代码被编译和执行时，它会产生下列结果：</p>
<pre>
50
</pre>
<h2>const 关键字</h2>
<p>您可以使用 <b>const</b> 前缀声明指定类型的常量，如下所示：</p>
<pre>
const type variable = value;
</pre>
<p>具体请看下面的实例：</p>
<div class="example"> 
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-reserved">using</span><span class="hl-code"> </span><span class="hl-types">namespace</span><span class="hl-code"> </span><span class="hl-identifier">std</span><span class="hl-code">;
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code">  </span><span class="hl-identifier">LENGTH</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
   </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code">  </span><span class="hl-identifier">WIDTH</span><span class="hl-code">  = </span><span class="hl-number">5</span><span class="hl-code">;
   </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">char</span><span class="hl-code"> </span><span class="hl-identifier">NEWLINE</span><span class="hl-code"> = '\</span><span class="hl-identifier">n</span><span class="hl-code">';
   </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">area</span><span class="hl-code">;  
   
   </span><span class="hl-identifier">area</span><span class="hl-code"> = </span><span class="hl-identifier">LENGTH</span><span class="hl-code"> * </span><span class="hl-identifier">WIDTH</span><span class="hl-code">;
   </span><span class="hl-identifier">cout</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">area</span><span class="hl-code">;
   </span><span class="hl-identifier">cout</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">NEWLINE</span><span class="hl-code">;
   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-number">0</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div>
</div>
</div>
<p>当上面的代码被编译和执行时，它会产生下列结果：</p>
<pre>
50
</pre>
<p>请注意，把常量定义为大写字母形式，是一个很好的编程实践。</p>
<br />			<!-- 其他扩展 -->
						
			</div>
			
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link"><i style="font-size:16px;" class="fa fa-arrow-left" aria-hidden="true"></i> <a href="../cplusplus/cpp-variable-scope.html" rel="prev"> C++ 变量作用域</a> </div>
			<div class="next-design-link"><a href="../cplusplus/cpp-modifier-types.html" rel="next"> C++ 修饰符类型</a> <i style="font-size:16px;" class="fa fa-arrow-right" aria-hidden="true"></i></div>
		</div>
		<!-- 笔记列表 -->
		<style>
.wrapper {
  /*text-transform: uppercase; */
  background: #ececec;
  color: #555;
  cursor: help;
  font-family: "Gill Sans", Impact, sans-serif;
  font-size: 20px;
  position: relative;
  text-align: center;
  width: 200px;
  -webkit-transform: translateZ(0); /* webkit flicker fix */
  -webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}

.wrapper .tooltip {
  white-space: nowrap;
  font-size: 14px;
  text-align: left;
  background: #96b97d;
  bottom: 100%;
  color: #fff;
  display: block;
  left: -25px;
  margin-bottom: 15px;
  opacity: 0;
  padding: 14px;
  pointer-events: none;
  position: absolute;
  
  -webkit-transform: translateY(10px);
     -moz-transform: translateY(10px);
      -ms-transform: translateY(10px);
       -o-transform: translateY(10px);
          transform: translateY(10px);
  -webkit-transition: all .25s ease-out;
     -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
       -o-transition: all .25s ease-out;
          transition: all .25s ease-out;
  -webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
     -moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
      -ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
       -o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
          box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
	color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
  bottom: -20px;
  content: " ";
  display: block;
  height: 20px;
  left: 0;
  position: absolute;
  width: 100%;
}  

/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
  border-left: solid transparent 10px;
  border-right: solid transparent 10px;
  border-top: solid #96b97d 10px;
  bottom: -10px;
  content: " ";
  height: 0;
  left: 20%;
  margin-left: -13px;
  position: absolute;
  width: 0;
}
.wrapper .tooltip1 {
	margin-left: 50px;
	padding-top: 0px;
}
/*
.wrapper:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
  -webkit-transform: translateY(0px);
     -moz-transform: translateY(0px);
      -ms-transform: translateY(0px);
       -o-transform: translateY(0px);
          transform: translateY(0px);
}
*/
/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
  display: none;
}

.lte8 .wrapper:hover .tooltip {
  display: block;
}

</style>

<link rel="stylesheet" href="../0/upvotejs.css">
<script src=""></script>
<script src=""></script>
<div class="title" id="comments">
	<h2 class="">
    <div class="altblock">
				<i style="font-size:28px;margin-top: 8px;" class="fa fa-plus-square" aria-hidden="true"></i>
		    </div>
    <span class="mw-headline" id="qa_headline">13  篇笔记</span>
	<span class="mw-headline" id="user_add_note" style="float:right;line-height: 62px;padding-right: 14px;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  写笔记</span>
    </h2>
</div>

<div id="postcomments"  style="display:none;" >
	<ol class="commentlist">
		<li class="comment even thread-even depth-1" id="comment-11267"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;绝罗</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;419***405@qq.com</p></div><div id="runoobvote-id-11267" data-commid = "11267" class="upvotejs"><a class="upvote"></a> <span class="count">168</span></div></div><div class="comt-main" id="div-comment-11267"><p>定义成 const 后的常量，程序对其中只能读不能修改。</p>
</p>
<p>以下程序是错误的，因为开头就已经固定了常量，便不能再对其进行赋值：</p>
<pre>
#include &lt;iostream&gt;
using namespace std;
int main()
{
    const double pi;                      //圆周率的值用pi表示
    pi=3.14159265;
    cout&lt;&lt;"圆周率的近似值是"&lt;&lt;pi&lt;&lt;endl;
    return 0;
}
</pre>

<p>
下面给出正确的赋值方法:
</p><pre>
#include &lt;iostream&gt;
using namespace std;
int main()
{
    const double pi=3.141592;            //圆周率的值用pi表示
    cout&lt;&lt;"圆周率的近似值是"&lt;&lt;pi&lt;&lt;endl;
    return 0;
}
</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">绝罗</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;绝罗</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;419***405@qq.com</p></div></span>6年前 (2017-04-21)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-12963"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;sunyw</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;934***022@qq.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="http://www.cnblogs.com/scut-linmaojiang/p/4722338.html">&nbsp;&nbsp;参考地址</a></p></div><div id="runoobvote-id-12963" data-commid = "12963" class="upvotejs"><a class="upvote"></a> <span class="count">344</span></div></div><div class="comt-main" id="div-comment-12963"><h2 data-title="C++ 宏定义 #define 和常量 const 的区别" data-commid="41214">宏定义 #define 和常量 const 的区别
</h2>
<p><strong>类型和安全检查不同</strong></p>

<p>宏定义是字符替换，没有数据类型的区别，同时这种替换没有类型安全检查，可能产生边际效应等错误；</p>

<p>const常量是常量的声明，有类型区别，需要在编译阶段进行类型检查</p>

<p><strong>编译器处理不同</strong></p>

<p>宏定义是一个"编译时"概念，在预处理阶段展开，不能对宏定义进行调试，生命周期结束与编译时期；</p>

<p>const常量是一个"运行时"概念，在程序运行使用，类似于一个只读行数据</p>

<p><strong>存储方式不同</strong></p>

<p>宏定义是直接替换，不会分配内存，存储于程序的代码段中；</p>

<p>const常量需要进行内存分配，存储于程序的数据段中</p>

<p><strong>定义域不同</strong></p>

<pre>void f1 ()
{
    #define N 12
    const int n 12;
}
void f2 ()
{
    cout&lt;&lt;N &lt;&lt;endl; //正确，N已经定义过，不受定义域限制
    cout&lt;&lt;n &lt;&lt;endl; //错误，n定义域只在f1函数中
}</pre>

<p><strong>定义后能否取消</strong></p>

<p>宏定义可以通过#undef来使之前的宏定义失效</p>

<p>const常量定义后将在定义域内永久有效</p>
<pre>
void f1()
{
  #define N 12
  const int n = 12;

  #undef N //取消宏定义后，即使在f1函数中，N也无效了
  #define N 21//取消后可以重新定义
}
</pre>
<p><strong>是否可以做函数参数</strong></p>

<p>宏定义不能作为参数传递给函数</p>

<p>const常量可以在函数的参数列表中出现</p><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="../note/12963">sunyw</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;sunyw</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;934***022@qq.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="http://www.cnblogs.com/scut-linmaojiang/p/4722338.html">&nbsp;&nbsp;参考地址</a></p></div></span>6年前 (2017-06-14)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-15416"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;飘逸的风</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;pia***139@aliyun.com</p></div><div id="runoobvote-id-15416" data-commid = "15416" class="upvotejs"><a class="upvote"></a> <span class="count">45</span></div></div><div class="comt-main" id="div-comment-15416"><p>1.const  定义常量之后，是不能够改变的</p>

<p>2.宏定义是可以取消的</p>

<pre>定义： #define    N    21
取消： #undef    N    12</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">飘逸的风</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;飘逸的风</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;pia***139@aliyun.com</p></div></span>6年前 (2017-07-26)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-16271"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;123</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;xuz***gsong@163.com</p></div><div id="runoobvote-id-16271" data-commid = "16271" class="upvotejs"><a class="upvote"></a> <span class="count">50</span></div></div><div class="comt-main" id="div-comment-16271"><p>const限定符定以后是不可以改变的，所以在定义时必须赋初始值，要不然是错误的，除非这个变量是用extern修饰的外部变量。 例如：</p>

<pre>const int A=10;       //正确。
const int A;          //错误，没有赋初始值。
extern const int A;   //正确，使用extern的外部变量。</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">123</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;123</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;xuz***gsong@163.com</p></div></span>6年前 (2017-08-13)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-26778"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;枫</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;318***194@qq.com</p></div><div id="runoobvote-id-26778" data-commid = "26778" class="upvotejs"><a class="upvote"></a> <span class="count">90</span></div></div><div class="comt-main" id="div-comment-26778"><p data-title="C++ const 关键字"data-commid="11267,15416, 16271"><strong>const关键字</strong></p><p><strong>const</strong>是<strong>constant</strong>的简写，只要一个变量前面用const来修饰，就意味着该变量里的数据可以被访问，不能被修改。也就是说const意味着只读（readonly）。</p><p><strong>规则：const离谁近，谁就不能被修改；</strong></p><p>const修饰一个变量，一定要给这个变量初始化值，若不初始化，后面就无法初始化。</p><p><strong>本质：const在谁后面谁就不可以修改，const在最前面则将其后移一位，二者等效。</strong></p><p><strong>const关键字作用</strong></p><ul><li>为给读你代码的人传达非常有用的信息，声明一个参数为常量是为了告诉用户这个参数的应用目的；</li><li>通过给优化器一些附加信息，使关键字const也许能产生更紧凑的代码；</li><li>合理使用关键字const可以使编译器很自然的保护那些不希望被修改的参数，防止无意的代码修改，可以减少bug的出现；</li></ul><p><strong>const关键字应用</strong></p><ul><li>欲阻止一个变量被改变，可使用const，在定义该const变量时，需先初始化，以后就没有机会改变他了；</li><li>对指针而言，可以指定指针本身为const，也可以指定指针所指的数据为const，或二者同时指定为const；</li><li>在一个函数声明中，const可以修饰形参表明他是一个输入参数，在函数内部不可以改变其值；</li><li>对于类的成员函数，有时候必须指定其为const类型，表明其是一个常函数，不能修改类的成员变量；</li><li>对于类的成员函数，有时候必须指定其返回值为const类型，以使得其返回值不为“左值”。</li></ul><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="../note/26778">枫</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;枫</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;318***194@qq.com</p></div></span>5年前 (2018-04-28)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-35629"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;夜月止水</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;925***909@qq.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="../w3cnote/const-char.html">&nbsp;&nbsp;参考地址</a></p></div><div id="runoobvote-id-35629" data-commid = "35629" class="upvotejs"><a class="upvote"></a> <span class="count">161</span></div></div><div class="comt-main" id="div-comment-35629"><p><b>const char*, char const*的区别</b></p>


<p>Bjarne 在他的 The C++ Programming Language 里面给出过一个助记的方法： <strong>把一个声明从右向左读</strong>。 </p>

<pre>char  * const cp; ( * 读成 pointer to ) 
cp is a const pointer to char 

const char * p; 
p is a pointer to const char; 

char const * p;</pre>
<p>同上因为 C++ 里面没有 const* 的运算符，所以 const 只能属于前面的类型。 </p>


<blockquote>更多内容查看：<a href="../w3cnote/const-char.html" target="_blank" rel="noopener nofollow">const char*, char const*, char*const 的区别</a></blockquote>

<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">夜月止水</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;夜月止水</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;925***909@qq.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="../w3cnote/const-char.html">&nbsp;&nbsp;参考地址</a></p></div></span>5年前 (2018-10-06)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-36490"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;adults</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;223***32@qq.com</p></div><div id="runoobvote-id-36490" data-commid = "36490" class="upvotejs"><a class="upvote"></a> <span class="count">22</span></div></div><div class="comt-main" id="div-comment-36490"><p>常量实例：已知半径，求圆的周长和面积。</p>

<pre>#include&lt;iostream&gt;   
using namespace std;

#define PI 3.14

int main()
{
    float radius = 10.0;
    float area = PI * radius * radius;
    float circumference = 2 * PI * radius;
    cout &lt;&lt; "area is "&lt;&lt;area &lt;&lt;", circumference is "&lt;&lt; circumference&lt;&lt;endl;
    return 0;
}</pre>
<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">adults</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;adults</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;223***32@qq.com</p></div></span>5年前 (2018-11-01)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-36587"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;WXGZCC</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;192***1103@qq.com</p></div><div id="runoobvote-id-36587" data-commid = "36587" class="upvotejs"><a class="upvote"></a> <span class="count">33</span></div></div><div class="comt-main" id="div-comment-36587"><p>const 和 define 定义常量时，两个数的范围不同。</p>

<pre>#include&lt;cstdio&gt;
using namespace std;
const int  maxn=1e+10;
int main()
{
    printf("%d",maxn);
    return 0;
}</pre>
<p>然后输出是：</p>

<pre>2147483647</pre>

<p>如果用 define 定义的话:</p>

<pre>#include&lt;cstdio&gt;
#define maxn 1e+10
using namespace std;
int main()
{
    printf("%d",maxn);
    return 0;
}</pre>
<p>输出是：</p>

<pre>536870912</pre>

<p>但是，如果将输出的 <strong>%d</strong> 改为 <strong>%lld</strong> 的话，输出为：</p>

<pre>4756540486875873280</pre>

<p>那么，可以思考一下，如果将 <strong>const</strong> 的数据也改成 <strong>longlong</strong> 的话，输出为：</p>



<pre>10000000000</pre>

<p>刚好为我们想要的那个答案。</p>

<p>数据的范围还有输出的格式很重要！！！</p><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">WXGZCC</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;WXGZCC</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;192***1103@qq.com</p></div></span>5年前 (2018-11-04)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-38396"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;初出茅庐的攻城狮</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;yan***000@126.com</p></div><div id="runoobvote-id-38396" data-commid = "38396" class="upvotejs"><a class="upvote"></a> <span class="count">58</span></div></div><div class="comt-main" id="div-comment-38396"><p>预处理 <span class="marked">#define</span>   变量定义值以后，不能用分号，否则就会计算错误，但是程序不会报错。</p><pre>#define age  12
#define age1 10

#define age2  12;
#define age3 10;

 

int main()
{
   int dd  ; 
   dd = age + age1;
   cout  &lt;&lt; "值=" &lt;&lt; dd &lt;&lt; endl; //值22
   dd = age2 + age3;
   cout  &lt;&lt; "值=" &lt;&lt; dd &lt;&lt; endl; //值12
    return 0;  
}</pre>

预处理 #define   变量定义值以后，不能用分号，否则就会计算错误，但是程序不会报错。<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">初出茅庐的攻城狮</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;初出茅庐的攻城狮</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;yan***000@126.com</p></div></span>4年前 (2018-12-27)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-41214"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;smallhu</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;107***3783@qq.com</p></div><div id="runoobvote-id-41214" data-commid = "41214" class="upvotejs"><a class="upvote"></a> <span class="count">64</span></div></div><div class="comt-main" id="div-comment-41214"><p><strong>角度1</strong>： 就定义常量说的话， const 定义的常数是变量 也带类型， #define 定义的只是个常数 不带类型。</p>

<p><strong>角度2</strong>： 就起作用的阶段而言，#define 是在编译的预处理阶段起作用，而 const 是在 编译、运行的时候起作用。</p>

<p><strong>角度3</strong>： 就起作用的方式而言，#define 只是简单的字符串替换，没有类型检查。而 const 有对应的数据类型，是要进行判断的，可以避免一些低级的错误。 正因为 define 只是简单的字符串替换会导致边界效应，具体举例可以参考下面代码：</p>

<pre>#define N 2+3     // 我们预想的 N 值是 5，我们这样使用 
Ndouble a = N/2;  // 我们预想的  a 的值是 2.5，可实际上 a 的值是 3.5</pre>
<p><strong>角度4</strong>： 就空间占用而言， 例如：</p>

<pre>#define PI 3.14     //预处理后 占用代码段空间
const float PI=3.14;    // 本质上还是一个 float，占用数据段空间</pre>      
<p><strong>角度5</strong>： 从代码调试的方便程度而言, const 常量可以进行调试的，#define 是不能进行调试的，因为在预编译阶段就已经替换掉了</p>

<p><strong>角度6</strong>： 从是否可以再定义的角度而言, const 不足的地方，是与生俱来的，const 不能重定义，而 #define 可以通过 #undef 取消某个符号的定义，再重新定义。</p><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">smallhu</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;smallhu</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;107***3783@qq.com</p></div></span>4年前 (2019-03-26)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-55996"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Amanda</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;146***9601@qq.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="https://www.yanbinghu.com/2019/01/28/7442.html">&nbsp;&nbsp;参考地址</a></p></div><div id="runoobvote-id-55996" data-commid = "55996" class="upvotejs"><a class="upvote"></a> <span class="count">89</span></div></div><div class="comt-main" id="div-comment-55996"><p><strong>修饰指针</strong></p><p>
修饰指针的情况比较多，主要有以下几种情况：</p>
<p>1、const 修饰 <strong>*p</strong>，指向的对象只读，指针的指向可变：</p>

<pre>int a = 9;
int b = 10;
const int *p = &amp;a;//p是一个指向int类型的const值,与int const *p等价
*p = 11;    //编译错误，指向的对象是只读的，不可通过p进行改变
p = &amp;b;     //合法，改变了p的指向</pre><p>
这里为了便于理解，可认为const修饰的是 *p，通常使用 <strong>*</strong> 对指针进行解引用来访问对象，因而，该对象是只读的。</p>
<p>
2、const 修饰 p，指向的对象可变，指针的指向不可变：</p>

<pre>int a = 9;
int b = 10;
int * const p = &amp;a;//p是一个const指针
*p = 11;    //合法，
p = &amp;b;     //编译错误，p是一个const指针，只读，不可变</pre>
<p>3、指针不可改变指向，指向的内容也不可变</p>

<pre>int a = 9;
int b = 10;
const int * const p = &amp;a;//p既是一个const指针，同时也指向了int类型的const值
*p = 11;    //编译错误，指向的对象是只读的，不可通过p进行改变
p = &amp;b;     //编译错误，p是一个const指针，只读，不可变</pre><p>
看完上面几种情况之后是否会觉得混乱，并且难以记忆呢？我们使用一句话总结：</p><p>
const 放在 * 的左侧任意位置，限定了该指针指向的对象是只读的；const放在 * 的右侧，限定了指针本身是只读的，即不可变的。</p>
<p>
如果还不是很好理解，我们可以这样来看，去掉类型说明符，查看  const修饰的内容，上面三种情况去掉类型说明符 int 之后，如下：</p>

<pre>const *p; //修饰*p，指针指向的对象不可变
* const p; //修饰p，指针不可变
const * const p; //第一个修饰了*p，第二个修饰了p，两者都不可变</pre>
<p>const 右边修饰谁，就说明谁是不可变的。上面的说法仅仅是帮助理解和记忆。借助上面这种理解，就会发现以下几种等价情况：</p>

<pre>const int NUM = 10; //与int const NUM等价
int a = 9;
const int *p  = &amp;a;//与int const *p等价
const int arr[] = {0,0,2,3,4}; //与int const arr[]等价</pre>
<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">Amanda</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Amanda</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;146***9601@qq.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="https://www.yanbinghu.com/2019/01/28/7442.html">&nbsp;&nbsp;参考地址</a></p></div></span>3年前 (2020-08-06)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-62028"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;HRZGJ</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;130***5956@qq.com</p></div><div id="runoobvote-id-62028" data-commid = "62028" class="upvotejs"><a class="upvote"></a> <span class="count">45</span></div></div><div class="comt-main" id="div-comment-62028"><p>1、const 关键字出现在 <span class="marked">*</span> 的左边：指针指向的内容不能被修改。</p>
<p>
2、const 关键字出现在 <span class="marked">*</span> 的右边：指针本身不能被修改。</p>
<p>
3、const 关键字出现在 <span class="marked">*</span> 的两边：指针指向的内容和指针本身都不能被修改。</p>

<pre>#include &lt;iostream&gt;
using namespace std;
int main()
{
    int a=1;
    int b;
    
    /**指向const的指针，指针指向的内容不能被修改**/
    const int *p1;
    int const *p2;
   
    /**const指针，指针本身不能被修改，必须初始化**/
    int *const p3=&amp;a;
    
     /*指针本身和它指向的内容都是不能被改变的所以也得初始化*/
    const int* const p4=&amp;a;
    int const* const p5=&amp;b; 
    
     p1=p2=&amp;a; //true  (指针本身的值可以改变)
    *p1=*p2=8; //false（指针指向的内容不能被修改）
    
    *p3=5; //true (指针指向的内容可以改变)
     p3=p1; //false（指针本身的值不能改变） 
    
     p4=p5;//false（指针本身和它指向的内容都是不能被改变） 
    *p4=*p5=4; //false（指针本身和它指向的内容都是不能被改变） 
  
    return 0;
}</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">HRZGJ</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;HRZGJ</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;130***5956@qq.com</p></div></span>2年前 (2021-03-09)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-76240"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Shmily</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;331***1182@qq.com</p></div><div id="runoobvote-id-76240" data-commid = "76240" class="upvotejs"><a class="upvote"></a> <span class="count">8</span></div></div><div class="comt-main" id="div-comment-76240"><p>const 关键字出现在 * 的左边：指针指向的内容不能被修改。称其为指向常量打的指针，此时也被称为底层const</p><p>const 关键字出现在 * 的右边：指针本身不能被修改，称其为常量指针，此时也被称为顶层const，</p><p>const 关键字出现在 * 的两边: 指针指向的内容和指针本身都不能被修改。此时同时拥有底层和顶层const。</p><p>几点注意：</p><p>（1）底层指针指向的内容并非不可修改，只是无法通过该指针进行修改，换句话来说，底层指针“自作多情”地认为自己指向的内容是无法修改的，实际上可以通过赋值等方法改变其指向的内容。</p><p>（2）在使用中，顶层const只用来修饰指针，无法修饰引用（因为引用不是变量）；而底层const可以修饰指针和引用，其意义为指向或引用的内容是一个常量。只需要记住，底层const限定指向或引用的对象是常量，对指针或引用并没有限定；顶层const限定指针是常量，对其指向的内容没有限定。</p><p>（3）底层const会给变量施加一个限制，在执行拷贝操作时，非底层const的对象无法给具有底层const的变量赋值。</p><pre>
int main()
{
    int a = 1;
    const int *p1 = &amp;a;         //底层const, 也可以用int const *p1 = &amp;a;.
    *p1 = 2;                    //非法, 无法通过p1更改a的值.
    p1++;                       //合法, p1的值(指向的地址)是可以更改的.
    a = 2;                      //合法, 只是无法通过p1解引用的方式更改a的值, 其他方式可以.

    int *const p2 = &amp;a;         //顶层const, p2是常量.
    *p2 = 3;                    //合法, p2指向地址的内容可以更改.
    p2++;                       //非法, p2的值(指向的地址)无法更改.
    a = 4;                      //合法, p2指向地址的内容可以更改.
    const int *const p3 = &amp;a;   //同时具有底层和顶层const, p3的值(指向的地址)无法修改, 且无法通过p3解引用进行更改.
    int *p4 = p1;               //非法, p1是const *int(底层const), 无法给普通int*赋值.
    int *p5 = p2;               //合法, p2是*const int(顶层const), 赋值时没有限制.
    const int *p6 = p1;         //合法, p6也是const *int(底层const), 可以获得p1的赋值.
}
</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">Shmily</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Shmily</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;331***1182@qq.com</p></div></span>6个月前 (11-07)</div></div></li><!-- #comment-## -->
	</ol>
	<div class="pagenav">
			</div>
</div>
<div id="respond" class="no_webshot"> 
		<div class="comment-signarea" style="display:none; padding: 20px 20px;"> 
	<h3 class="text-muted" id="share_code" style="color: #799961;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 点我分享笔记</h3>
	<!--
	<p style="font-size:14px;">笔记需要是本篇文章的内容扩展！</p><br>
	<p style="font-size:12px;"><a href="../tougao" target="_blank">文章投稿，可点击这里</a></p>
	<p style="font-size:14px;"><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>
		<h3 class="text-muted"><i class="fa fa-info-circle" aria-hidden="true"></i> 分享笔记前必须<a href="javascript:;" class="runoob-pop">登录</a>！</h3>
		<p><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>-->
	</div>
		
	<form action="/wp-content/themes/runoob/option/addnote.php" method="post" id="commentform" style="display:none;">
		<div class="comt">
			<div class="comt-title">
				<i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i>				<p><a id="cancel-comment-reply-link" href="javascript:;">取消</a></p>
			</div>
			<div class="comt-box">
			<div id="mded"></div>
			
				<div class="comt-ctrl">
					<div class="comt-tips"><input type='hidden' name='comment_post_ID' value='11174' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</div>
					<button type="submit" name="submit" id="submit" tabindex="5"><i class="fa fa-pencil" aria-hidden="true"></i> 分享笔记</button>
				</div>
			</div>
		
				
					<div class="comt-comterinfo"> 
						<ul id="comment-author-info">
							<li class="form-inline"><label class="hide" for="author">昵称</label><input class="ipt" type="text" name="author" id="author" value="" tabindex="2" placeholder="昵称"><span class="text-muted">昵称 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="email">邮箱</label><input class="ipt" type="text" name="email" id="email" value="" tabindex="3" placeholder="邮箱"><span class="text-muted">邮箱 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="url">引用地址</label><input class="ipt" type="text" name="url" id="url" value="" tabindex="4" placeholder="引用地址"><span class="text-muted">引用地址</span></li>
						</ul>
					</div>
				
			
		</div>

	</form>
	</div>
<script type="text/javascript">
$(function() {
	//初始化编辑器
	
	var editor = new Simditor({
	  textarea: $('#mded'),
	  placeholder: '写笔记...',
	  upload:false,
	 // upload: {url:'/api/comment_upload_file.php',params: null,fileKey: 'upload_file',connectionCount: 1,leaveConfirm: '文件正在上传，您确定离开?'},
	  defaultImage: 'https://www.runoob.com/images/logo.png',
	  codeLanguages: '',
	  autosave: 'editor-content',
	  toolbar: [  'bold','code','ul','ol','image' ]
	});
	editor.on('selectionchanged', function() {
		$(".code-popover").hide();
	});

	// 提交数据
	$("#share_code").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		
	});
	$("#user_add_note").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		$('html, body').animate({
       	    scrollTop: $("#respond").offset().top
    	}, 200);
	});

	// 提交笔记
	var commentform=$('#commentform');
	commentform.prepend('<div id="comment-status" style="display:none;" ></div>');
	var statusdiv=$('#comment-status');
	
	commentform.submit(function(e){
		e.preventDefault();
		var noteContent = editor.getValue();
		// console.log(noteContent);
		noteContent = noteContent.replace(/<pre><code>/g,"<pre>");
		noteContent = noteContent.replace(/<\/code><\/pre>/g,"</pre>");
		
		// 系列化表单数据
		var comment_parent = 0;
		var is_user_logged_in = $("#is_user_logged_in").val();
		var comment_post_ID =  11174;
		var _wp_unfiltered_html_comment = $("#_wp_unfiltered_html_comment").val();
		var comment = noteContent;
		var author = $("#author").val();
		var url = $("#url").val();
		var email = $("#email").val();
		if(isBlank(author) && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入昵称！</p>').show();
		} else if(isBlank(email)  && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入邮箱！</p>').show();
		} else {
			// var formdata=commentform.serialize() + "&comment=" + noteContent ;
			// 添加状态信息
			statusdiv.html('<p>Processing...</p>').show();
			// 获取表单提交地址
			var formurl=commentform.attr('action');
			
			// 异步提交
			$.ajax({
					type: 'post',
					url: formurl,
					dataType:'json',
					data: {"comment_parent":comment_parent,"comment_post_ID":comment_post_ID, "_wp_unfiltered_html_comment":_wp_unfiltered_html_comment,"comment":comment,"url":url, "email":email,"author":author},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					statusdiv.html('<p class="ajax-error" >数据不完整或表单提交太快了！</p>').show();
				},
				success: function(data, textStatus){
					if(data.errorno=="0") {
						$("#submit").prop('disabled', true);
						statusdiv.html('<p class="ajax-success" >笔记已提交审核，感谢分享笔记！</p>').show();
						alert('笔记已提交审核，感谢分享笔记！');
					}else{
						statusdiv.html('<p class="ajax-error" >'+data.msg+'</p>').show();
					}
					commentform.find('textarea[name=comment]').val('');
				}
			});
			setTimeout(function(){
		        $("#submit").prop('disabled', false);
		    }, 10*1000);
		}
		return false;

	});
	$(".comt-author").click(function() {
		href = $(this).children("a").attr("href");
		if(href.indexOf("/note/")!=-1) {
			var win = window.open(href, '_blank');
  			win.focus();
		}
	});
	$(".comt-meta span").hover(function(){
		$(this).children(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).children(".tooltip").removeAttr("style");
	});
	/*
	$(".wrapper i").hover(function(){
		$(this).siblings(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).siblings(".tooltip").css({ "opacity": 0, "pointer-events": "auto"});
	});
	*/
	//Upvote.create('runoobvote-id', {callback: vote_callback});
	var ajaxurl = 'https://www.runoob.com/wp-admin/admin-ajax.php';
	var callback = function(data) {
		//console.log($('#runoobvote-id').upvote('upvoted'));
		//console.log($('#runoobvote-id').upvote('downvoted'));
		//console.log(data);
		_vote_action = data.action;
		id_arr = data.id.split('-');
		um_id= id_arr[2];
		//console.log(um_id);
		
		var re = /^[1-9]+/;
		if (re.test(um_id)) { 
			var ajax_data = {
				_vote_action: _vote_action,
				action: "pinglun_zan",
				um_id: um_id,
				um_action: "ding"
			};
			//console.log(ajax_data);
			$.post(ajaxurl,ajax_data,function(status){
				//if(status.vote_num>999) {
				//	_voteHtml = '<span style="display: block; text-align: center;font-size: 20px; color: #6a737c; margin: 8px 0;">'+kFormatter(status.vote_num) +'</span>';
				//	$("#runoobvote-id-" + um_id + " .count").hide().after(_voteHtml);
				//}
				
			});
		}
	};
	if($('#comments').length && $('.upvotejs').length){
		$('.upvotejs').upvote({id: 11174, callback: callback});
	
		$.post(ajaxurl,{"action":"pinglun_zan","postid":11174},function(data){  
			$(data).each(function(key,value) {
				$("#runoobvote-id-" + value.commid + " .upvote").addClass(value.upvotejs_class);
				$("#runoobvote-id-" + value.commid + " .downvote").addClass(value.downvote_class);
				$("#runoobvote-id-" + value.commid + " .count").text(value.upvote_count);
			})
		},'json');
		
	}
	
	
});
function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
function kFormatter(num) {
	// return num;
    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}

</script>

<link rel="stylesheet" href="../0/qa.css?1.44">
<link rel="stylesheet" type="text/css" href="../0/simditor.min.css" />
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
		<div class="sidebar-box ">
				

<style>	
.responsive_ad1 { display:none; }
@media(min-width: 800px) { .responsive_ad1 {  display:block;margin:0 auto;} }
</style>
<div id="ad-336280" >

<script async src=""
     crossorigin="anonymous"></script>
<!-- 移动版 自动调整 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-5751451760833794"
     data-ad-slot="1691338467"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
		</div>
				</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">

<style>
.sidebar-tree .double-li {
    width:300px;
}
.sidebar-tree .double-li li {
    width: 44%;
    line-height: 1.5em;
    border-bottom: 1px solid #ccc;
    float: left;
    display: inline;
}
</style>

    
        <div class="sidebar-box re-box re-box-large">
        <div class="sidebar-box recommend-here" style="margin: 0 auto;">
            <a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;"> <i class="fa fa-list" aria-hidden="true"></i> 分类导航</a>
        </div>
    <div class="sidebar-box sidebar-cate">
        
        <div class="sidebar-tree" >
            <ul><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> HTML / CSS</a><ul class="double-li"><li><a title="HTML 教程" href="../html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="../html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="../css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="../css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap3 教程" href="../bootstrap/bootstrap-tutorial.html">Bootstrap3 教程</a></li><li><a title="Bootstrap4 教程" href="../bootstrap4/bootstrap4-tutorial.html">Bootstrap4 教程</a></li><li><a title="Bootstrap5 教程" href="../bootstrap5/bootstrap5-tutorial.html">Bootstrap5 教程</a></li><li><a title="Font Awesome 教程" href="../font-awesome/fontawesome-tutorial.html">Font Awesome 教程</a></li><li><a title="Foundation 教程" href="../foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> JavaScript</a><ul class="double-li"><li><a title="JavaScript 教程" href="../js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="../htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="../jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="../angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="AngularJS2 教程" href="../angularjs2/angularjs2-tutorial.html">AngularJS2 教程</a></li><li><a title="Vue.js 教程" href="../vue2/vue-tutorial.html">Vue.js 教程</a></li><li><a title="Vue3 教程" href="../vue3/vue3-tutorial.html">Vue3 教程</a></li><li><a title="React 教程" href="../react/react-tutorial.html">React 教程</a></li><li><a title="TypeScript 教程" href="../typescript/ts-tutorial.html">TypeScript 教程</a></li><li><a title="jQuery UI 教程" href="../jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="../jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="../nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="../ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="../json/json-tutorial.html">JSON 教程</a></li><li><a title="Echarts 教程" href="../echarts/echarts-tutorial.html">Echarts 教程</a></li><li><a title="Chart.js 教程" href="../chartjs/chartjs-tutorial.html">Chart.js 教程</a></li><li><a title="Highcharts 教程" href="../highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="../googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 服务端</a><ul class="double-li"><li><a title="Python 教程" href="../python3/python3-tutorial.html">Python 教程</a></li><li><a title="Python2.x 教程" href="../python/python-tutorial.html">Python2.x 教程</a></li><li><a title="Linux 教程" href="../linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="../docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="../ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="../java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="../c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="../cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="../perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="../servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="../jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="../lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Rust 教程" href="../rust/rust-tutorial.html">Rust 教程</a></li><li><a title="Scala 教程" href="../scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="../go/go-tutorial.html">Go 教程</a></li><li><a title="PHP 教程" href="../php/php-tutorial.html">PHP 教程</a></li><li><a title="数据结构与算法" href="../data-structures/data-structures-tutorial.html">数据结构与算法</a></li><li><a title="Django 教程" href="../django/django-tutorial.html">Django 教程</a></li><li><a title="Zookeeper 教程" href="../w3cnote/zookeeper-tutorial.html">Zookeeper 教程</a></li><li><a title="设计模式" href="../design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="../regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="Maven 教程" href="../maven/maven-tutorial.html">Maven 教程</a></li><li><a title="Verilog 教程" href="../w3cnote/verilog-tutorial.html">Verilog 教程</a></li><li><a title="ASP 教程" href="../asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="../appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="../vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 数据库</a><ul class="double-li"><li><a title="SQL 教程" href="../sql/sql-tutorial.html">SQL 教程</a></li><li><a title="MySQL 教程" href="../mysql/mysql-tutorial.html">MySQL 教程</a></li><li><a title="PostgreSQL 教程" href="../postgresql/postgresql-tutorial.html">PostgreSQL 教程</a></li><li><a title="SQLite 教程" href="../sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="../mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="../redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="../Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 数据分析</a><ul class="double-li"><li><a title="Python 教程" href="../python3/python3-tutorial.html">Python 教程</a></li><li><a title="NumPy 教程" href="../numpy/numpy-tutorial.html">NumPy 教程</a></li><li><a title="Pandas 教程" href="../pandas/pandas-tutorial.html">Pandas 教程</a></li><li><a title="Matplotlib 教程" href="../matplotlib/matplotlib-tutorial.html">Matplotlib 教程</a></li><li><a title="Scipy 教程" href="../scipy/scipy-tutorial.html">Scipy 教程</a></li><li><a title="R 教程" href="../r/r-tutorial.html">R 教程</a></li><li><a title="Julia 教程" href="../julia/julia-tutorial.html">Julia 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 移动端</a><ul class="double-li"><li><a title="Android 教程" href="../w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="../swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="../jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="../ionic/ionic-tutorial.html">ionic 教程</a></li><li><a title="Kotlin 教程" href="../kotlin/kotlin-tutorial.html">Kotlin 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> XML 教程</a><ul class="double-li"><li><a title="XML 教程" href="../xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="../dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="../dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="../xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="../xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="../xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="../xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="../xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="../schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="../xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="../svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> ASP.NET</a><ul class="double-li"><li><a title="ASP.NET 教程" href="../aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="../csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="../aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="../aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="../aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="../aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> Web Service</a><ul class="double-li"><li><a title="Web Service 教程" href="../webservices/webservices-tutorial.html">Web Service 教程</a></li><li><a title="WSDL 教程" href="../wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="../soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="../rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="../rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 开发工具</a><ul class="double-li"><li><a title="Eclipse 教程" href="../eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="../git/git-tutorial.html">Git 教程</a></li><li><a title="Svn 教程" href="../svn/svn-tutorial.html">Svn 教程</a></li><li><a title="Markdown 教程" href="../markdown/md-tutorial.html">Markdown 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 网站建设</a><ul class="double-li"><li><a title="HTTP 教程" href="../http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="../web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="../browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="../hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="../tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="../w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="../quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>            </div>
    
    </div>
    </div>
    <br>
    
    <div class="sidebar-box re-box re-box-large">
        <div class="sidebar-box recommend-here">
            <a href="javascript:void(0);">Advertisement</a>
        </div>
        <div class="re-600160" id="sidebar-right-re">
                <script async src=""></script>
        <!-- 侧栏1 -->
        <ins class="adsbygoogle"
             style="display:inline-block;width:160px;height:600px"
             data-ad-client="ca-pub-5751451760833794"
             data-ad-slot="4106274865"></ins>
        <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
        </script>
                </div>
    </div>
</div></div>

</div>

<script>
var aid = 11174;
function coll() {
	$.post( '/wp-content/themes/runoob/option/user/userinfo.php', {aid:aid, action:"collarticle", opt:'add'},function( data ) {
		if(data.error==0) {
			$("#content").find("h1:first").find("a").attr("href","javascript:void(0);");
			$("#content").find("h1:first").find("img").attr("src","http://www.runoob.com/wp-content/themes/runoob/assets/img/coll2.png").css({width:32+"px",height:32+"px"});
		}
		alert(data.msg);
	},'json');
}
</script>


<!-- 反馈对话框开始 -->
<script src=""></script>
<link rel="stylesheet" href="../0/feedback.css?1.0" />
<script type="text/javascript">
$.feedback({
    ajaxURL: '/feedback/runoob_feedback.php',
	html2canvasURL: '/wp-content/themes/runoob/assets/feedback/stable/2.0/html2canvas.js',
	onClose: function () {
         window.location.reload();
    }
});
</script>
<!-- 反馈对话框结束 -->
<button class="feedback-btn feedback-btn-gray">反馈/建议</button>
<!-- 底部 -->
<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a target="_blank" href="../html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a target="_blank" href="../jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a target="_blank" href="../charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6939/">JS 混淆/加密</a>
      </dd> 
      <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6232/">PNG/JPEG 图片压缩</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/6680/">随机数生成器</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imread.html" title="Matplotlib imread() 方法">Matplotlib imre...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imsave.html" title="Matplotlib imsave() 方法">Matplotlib imsa...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imshow.html" title="Matplotlib imshow() 方法">Matplotlib imsh...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-hist.html" title="Matplotlib 直方图">Matplotlib 直方图</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-func-object.html" title="Python object() 函数">Python object()...</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-ai-draw.html" title="Python AI 绘画">Python AI 绘画</a>
      </dd>
              <dd>
       &middot;
      <a href="../w3cnote/cursor-editor.html" title="神辅助 Cursor 编辑器，加入 GPT-4 让编码更轻松！">神辅助 Cursor ...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="mailto:admin@runoob.com" rel="external nofollow">意见反馈</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a target="_blank" href="../aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png" />
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2023    <strong><a href="../" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="../" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="display:none" href="javascript:void(0)"   title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png"></div> 
    </div>
  </div>

 <div style="display:none;">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-84264393-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>

<script src=""></script>
<script src=""></script>

</body>
</html>